import java.awt.Point;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class SecondChance extends Strategy {
	HashMap<Integer, Integer> memoria = new HashMap<Integer, Integer>();
	List<Point> accessCounter = new ArrayList<Point>();
	int bitR;

	public SecondChance(int numFrames, List<Request> requests, int bitR) {
		super(numFrames, requests);
		this.bitR = bitR;
	}

	void run() {
		int tempo = 0;

		for (Request op : requests) {
			if (tempo >= bitR) {
				for (Point p : accessCounter) {
					p.y = 0;
				}
				tempo = 0;

			}

			if (memoria.containsKey(op.value)) {
				for (Point p : accessCounter) {
					if (p.x == op.value) {
						p.y = 1;
						break;
					}
				}
				hit++;

			} else {
				if (memoria.size() >= frameSize) {
					int removed = -1;
					Point cache = null;

					while (removed == -1) {
						cache = accessCounter.remove(0);
						if (cache.y == 0) {
							removed = cache.x;
						} else {
							cache.y = 0;
							accessCounter.add(cache);
						}

					}
					memoria.remove(removed);
				}
				memoria.put(op.value, op.value);
				accessCounter.add(new Point(op.value, 1));
			}

			tempo++;

		}

	}

	static String getName() {
		return "SecondChance";
	}

	void reset() {
		memoria.clear();
		accessCounter.clear();
		hit = 0;
	}

}
